package com.example.sunsg.okhttptest.adapter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.breadtrip.R;
import com.example.sunsg.okhttptest.util.DisplayUtils;
import com.example.sunsg.okhttptest.util.Util;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by susng on 2016/5/10.
 */
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private Context mContext;
    private LayoutInflater mInflater;
    private List<Item> mItmes;
    private int tvWidth;
    /**方向*/
    private int mOriation = LinearLayoutManager.VERTICAL;
    public RecyclerViewAdapter(Context context) {
       this(context,LinearLayoutManager.VERTICAL);
    }
    public RecyclerViewAdapter(Context context,int oriation) {
        mContext = context;
        mInflater = LayoutInflater.from(mContext);
        mItmes = new ArrayList<>();
        tvWidth = (int) ((float)DisplayUtils.getScreenWidth(context)/4);
        mOriation = oriation;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        StringHolder holder = null;
        switch (viewType){
            case Item.TYPE_1:
                View view = mInflater.inflate(R.layout.item_activity_recycler_view,parent,false);
                holder= new StringHolder(view);
                break;
            case Item.TYPE_MORE:
                View view2 = mInflater.inflate(R.layout.item_activity_recycler_view,parent,false);
                holder= new StringHolder(view2);
                break;
            case Item.TYPE_THREE:
                View view3 = mInflater.inflate(R.layout.item_activity_recycler_view_3,parent,false);
                holder= new StringHolder(view3);
                break;
            case Item.TYPE_FOUR:
                View view4 = mInflater.inflate(R.layout.item_activity_recycler_view_4,parent,false);
                holder= new StringHolder(view4);
                break;
        }

        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        Item entry = (Item) getItem(position);
        StringHolder stringHolder = (StringHolder) holder;
        stringHolder.tvName.setText(entry.name);
        if(LinearLayoutManager.VERTICAL == mOriation){
            int height = (int) (Util.dp2px(mContext,50) * entry.random);
            RelativeLayout.LayoutParams parmas = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, height);
            stringHolder.tvName.setLayoutParams(parmas);
        }else if(LinearLayoutManager.HORIZONTAL == mOriation){
            RelativeLayout.LayoutParams parmas = new RelativeLayout.LayoutParams(tvWidth, ViewGroup.LayoutParams.MATCH_PARENT);
            stringHolder.tvName.setLayoutParams(parmas);
        }
        int type = entry.type;
        if(type == Item.TYPE_THREE){
            stringHolder.tvName.setText(entry.name);
            int height = (int) (Util.dp2px(mContext,50) );
            RelativeLayout.LayoutParams parmas = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, height);
            stringHolder.tvName.setLayoutParams(parmas);
        }


    }

    @Override
    public int getItemViewType(int position) {
        return ((Item)getItem(position)).type;
    }

    @Override
    public int getItemCount() {
        return mItmes == null ? 0 : mItmes.size();
    }

    public Object getItem(int position){
        return mItmes.get(position);
    }

    public void setItem(List<Item> items){
        this.mItmes = items;
        notifyDataSetChanged();
    }


    /***
     * layout item_activity_recycler_view
     */
    public static class StringHolder extends RecyclerView.ViewHolder{
        public TextView tvName;
        public RecyclerView recyclerView;
        private RecyclerViewItemAdapter adapter;
        public StringHolder(View itemView) {
            super(itemView);
            tvName = (TextView) itemView.findViewById(R.id.tv_name);
//            recyclerView = (RecyclerView) itemView.findViewById(R.id.recycle_view_item);

//            LinearLayoutManager manager = new LinearLayoutManager(mContext);
//            manager.setOrientation(LinearLayoutManager.HORIZONTAL);
//            RecyclerViewItemAdapter adapter = new RecyclerViewItemAdapter(mContext);
//            adapter.setItem(mItmes);
//            stringHolder.recyclerView.addItemDecoration(new RecyclerDividerItemDecoradion(mContext, 5, RecyclerDividerItemDecoradion.HORIZONTAL));
//            stringHolder.recyclerView.setAdapter(adapter);
        }

        public void setItem(List<String> itmes){
//            adapter.setItem(itmes);
        }
    }

    public static class ThreeHolder extends RecyclerView.ViewHolder{
        public TextView tvName;
        public ThreeHolder(View itemView) {
            super(itemView);
            tvName = (TextView) itemView.findViewById(R.id.tv_name);
        }
    }



    public static class Item{
        /*** 每行一个元素*/
        public final static int TYPE_1 = 1;
        /**每行多个元素*/
        public final static int TYPE_MORE = 2;
        /**第三种布局方式*/
        public final static int TYPE_THREE = 3;
        /**第四种布局方式*/
        public final static int TYPE_FOUR = 4;
        public String name;
        public float random;
        public int type;


        public boolean isLineFirst;//是否是每一行的第一个
        public boolean isLineLast;//是否是每一行的最后一个
    }
}
